//
//  OrderViewController.m
//  PickerView
//
//  Created by cocoazxj@163.com on 15/4/1.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "OrderViewController.h"

@interface OrderViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, strong) IBOutlet UIPickerView *pv_view;
@property (nonatomic, strong) IBOutlet UILabel *lb_fruit;
@property (nonatomic, strong) IBOutlet UILabel *lb_greens;
@property (nonatomic, strong) IBOutlet UILabel *lb_drink;
@property (nonatomic, strong) NSArray *mArray;
@end

@implementation OrderViewController

- (NSArray *)mArray
{
    if(!_mArray)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"foods" ofType:@".plist"];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        _mArray = array;
    }
    return _mArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    for (int i = 0; i < self.mArray.count; i++)
    {
        [self.pv_view selectRow:2 inComponent:i animated:YES];
        [self pickerView:nil didSelectRow:2 inComponent:i];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.mArray.count;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.mArray[component] count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.mArray[component][row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            self.lb_fruit.text = self.mArray[component][row];
            break;
 
        case 1:
            self.lb_greens.text = self.mArray[component][row];
            break;
            
        case 2:
            self.lb_drink.text = self.mArray[component][row];
            break;
            
        default:
            break;
    }
}

- (IBAction)autoButtonClicked:(id)sender
{
    for (int i = 0; i < self.mArray.count; i ++ )
    {
        int autoNum = arc4random()%[self.mArray[i] count];
        int oldNum = [self.pv_view selectedRowInComponent:i];
        while (autoNum == oldNum) {
            autoNum = arc4random()%[self.mArray[i] count];
        }
        [self.pv_view selectRow:autoNum inComponent:i animated:YES];
        [self pickerView:self.pv_view didSelectRow:autoNum inComponent:i];
    }
    
}
@end
